cc.Class({
    extends: cc.Component,

    properties: {

        //菜单
        menu : cc.Node,

        //管道预制件
        pipePrefab:cc.Prefab,

        //时间间隔
        pipe_spawn_interval:1.5,

        pipeNode : cc.Node,


        //结束文本
        gameoverLab: cc.Label,

        //小鸟节点
        bird: cc.Node,

        //地板节点
        ground: cc.Node,
        scoreLable: cc.Label,
        highscore: cc.Label,

        music: [cc.AudioClip],
    
    },


    onLoad: function () {

        //cc.audioEngine.playEffect(this.music,false );
        this.score = 0;
        this.isGameover = false;

    },

    //按钮点击回调
    onButtonClicked : function(x,customData){

      
        this.scoreLable.node.active = true;
        this.menu.active = false;
        this.schedule(this.spawnPipe.bind(this),this.pipe_spawn_interval)
        
    
    },

    addScore:function(){
        this.score++;
        this.scoreLable.string = this.score + '';
        var action1 = cc.scaleTo(0.1,0.8,1.2);
        var action2= cc.scaleTo(0.1,1.2,0.8);
        var action3 = cc.scaleTo(0.1,1,1);
        this.scoreLable.node.runAction(cc.sequence(action1,action2,action3));
    },



    gameover:function(){
        this.highScore = (cc.sys.localStorage.getItem("小鸟游戏的最高分") || 10);//返回的是字符串

        if(this.score > Number(this.highScore)){
            cc.sys.localStorage.getItem("小鸟游戏的最高分") || this.score ;
            this.highScore = this.score;

        }
        this.highscore.string = 'HIGHSCORE:' + this.highScore; 
        this.highscore.node.active = true;
        

        this.scoreLable.string = 'SCORE:' + this.score;


        //显示结束文本
        this.gameoverLab.node.active = true;  
        //让地板移动的计时器停止
        this.ground.parent.getComponent("Gronud").unscheduleAllCallbacks(); 
        //让控制小鸟的计时器停止
        this.bird.getComponent("Bird").unscheduleAllCallbacks(); 
        //管道生成计时器关闭
        //this.unscheduleAllCallbacks();

        this.bird.getComponent(cc.Animation).pause("fly");

        this.isGameover = true;

        this.scheduleOnce(() => {cc.director.loadScene("game")},3);
        //相当于function(){}
    },

    //生成管道
    spawnPipe:function(){

        var pipes = cc.instantiate( this.pipePrefab);
        this.pipeNode.addChild(pipes);
    },

    update:function(dt){
        if(this.bird.y - this.ground.y <= (this.bird.height + this.ground.height)/2){
           
            this.bird.y =  (this.bird.height + this.ground.height)/2 + this.ground.y;
            //this.bird.getComponent(cc.Animation).pause("fly");
            this.gameover();
            //让小鸟的动画停止
           
          }else if(this.bird.y + this.bird.height / 2 >= cc.winSize.height / 2){
            this.bird.y = cc.winSize.height / 2 - this.bird.height / 2;
            //this.bird.getComponent(cc.Animation).pause("fly");
            this.gameover();


          }

    },


});
